package json_param_util

import "strconv"

type JsonParam struct {
	_Objs map[string]any
}

func NewJsonParam(objs map[string]any) *JsonParam {
	return &JsonParam{_Objs: objs}
}

func (_param *JsonParam) GetAsAny(name string) any {
	return _param._Objs[name]
}

// 获取指定名称的字符串类型参数值
func (_param *JsonParam) GetAsString(name string) *string {
	value, ok := _param._Objs[name]
	if !ok || (value == nil) {
		return nil
	}

	result := value.(string)

	return &result
}

// 获取指定名称的布尔类型参数值
func (_param *JsonParam) GetAsBool(name string) *bool {
	value, ok := _param._Objs[name]
	if !ok || (value == nil) {
		return nil
	}

	result := value.(bool)

	return &result
}

// 获取指定名称的数字类型参数值
func (_param *JsonParam) GetAsNumber(name string) *float64 {
	value, ok := _param._Objs[name]
	if !ok || (value == nil) {
		return nil
	}

	var result float64

	switch t := value.(type) {
	case string:
		if t != "" {
			var err error
			result, err = strconv.ParseFloat(t, 64)

			if err != nil {
				panic(err)
			}
		} else {
			return nil
		}
	default:
		result = value.(float64)
	}

	return &result
}
